package com.niit.order.web;

import cn.hutool.json.JSONUtil;
import com.niit.feign.pojo.User;
import com.niit.order.service.MySource;
import com.niit.order.service.SinkSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yaojianfeng
 */
@RestController
@RequestMapping("/message")
public class OrderMessageController {

    @Autowired
    private SinkSender sinkSender;

    @Autowired
    private MessageChannel input;

    @Autowired
    @Qualifier("output1")
    private MessageChannel output1;

    @Autowired
    private MySource mySource;


    @RequestMapping("/sinkSender/{msg}")
    public String sinkSenderMessage(@PathVariable("msg")String msg){
        boolean send = sinkSender.output().send(MessageBuilder.withPayload(msg).build());
        if (send) {
            return "成功发送消息: " + msg;
        }
        return "消息发送失败: " + msg;
    }

    @RequestMapping("/inputChannel/{msg}")
    public String channelMessage(@PathVariable("msg")String msg){
        boolean send = input.send(MessageBuilder.withPayload(msg).build());
        if (send) {
            return "成功发送消息: " + msg;
        }
        return "消息发送失败: " + msg;
    }

    @RequestMapping("/userInfo/")
    public String output2ChannelMessage(User user){
        boolean send = mySource.output2().send(MessageBuilder.withPayload(JSONUtil.toJsonStr(user)).build());
        if (send) {
            return "成功发送消息: " + JSONUtil.toJsonStr(user);
        }
        return "消息发送失败: " + JSONUtil.toJsonStr(user);
    }

    @RequestMapping("/mysource/{msg}")
    public String mysourceMessage(@PathVariable("msg")String msg){
        boolean send = mySource.output1().send(MessageBuilder.withPayload(msg).build());
        if (send) {
            return "成功发送消息: " + msg;
        }
        return "消息发送失败: " + msg;
    }

    @RequestMapping("/output1/{msg}")
    public String output1Message(@PathVariable("msg")String msg){
        boolean send = output1.send(MessageBuilder.withPayload(msg).build());
        if (send) {
            return "成功发送消息: " + msg;
        }
        return "消息发送失败: " + msg;
    }
}
